package org.csu.mypetstore.controller;
//controller包用来实现原web的servlet功能，进行界面跳转1

import jakarta.servlet.http.HttpSession;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.vo.CategoryVO;
import org.csu.mypetstore.vo.ItemVO;
import org.csu.mypetstore.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/catalog")  // /catalog该路径在index.html中
public class CatalogController {

    @Autowired
    private CatalogService catalogService;

    @GetMapping("/view")  //  catalog/view
    public String view(HttpSession session){
        session.getAttribute("session.loginAccount");
        session.removeAttribute("session.loginAccount");
        return "catalog/main";
    }  //由index.html跳转到main

    @GetMapping("/viewCategory")
    public String viewCategory(String categoryId, Model model){
        CategoryVO categoryVO=catalogService.getCategory(categoryId);
        model.addAttribute("category",categoryVO);
        return "catalog/category";
        //vo类中的数据对应数据库表中的属性
        // vo层调用service层的方法，service层方法通过impl层来具体实现
        //impl层又调用mapper层
        // 通过添加attribute，将值映射到对应的html的标签中
    }

    @GetMapping("/viewProduct")
    public String viewProduct(String productId, Model model){
        ProductVO productVO=catalogService.getProduct(productId);
        model.addAttribute("product",productVO);
        return "catalog/product";
    }

    @GetMapping("/viewItem")
    public String viewItem(String itemId, Model model){
        ItemVO itemVO=catalogService.getItem(itemId);
        model.addAttribute("item",itemVO);
        return "catalog/item";
    }

}
